<?php
/*

  Copyright (c) 2008   by  maxim.oransky <maxim.oransky@gmail.com>,
  auhtor of  the-wheel-library  team

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

class HumanFormat {

	public static function date($time = false) {

		if (!$time) $time = time();

		$dd = date('j', $time);
		$mm = date('n', $time);
		$yy = date('Y', $time);
		$hm = date('H:i', $time);

		$res = $dd . ' ' . Tr::ofMonth($mm);

		$res = ($yy == date('Y')) ?
		  $res :
		  "$res ${yy} г.";

		if ($hm != '00:00') $res = $res . ', ' . $hm;

		return $res;
	}

	public static function numeral($num, $low, $mid, $hi, $space = null) {

	    if ($space === null) {
	        $space =  Env::isConsoleCall() ? ' ' : '&nbsp;';
	    }

		$lastDigit = $num % 10;
		$lastTwoDigits = $num % 100;

		if ((4 < $lastTwoDigits) && ($lastTwoDigits < 21)){
			$suf = $hi;
		} else if ($lastDigit == 1) {
			$suf = $low;
		} else if ((1 < $lastDigit) && ($lastDigit < 5)) {
			$suf = $mid;
		} else {
			$suf = $hi;
		}

		return $num . $space . $suf;
	}

	public static function filesize($size, $words = null) {

		if ($words === null)
			$words = array(' Kb', ' Mb', ' G');

		$bytesInGygabyte = 1073741824;
		$bytesInMegabyte = 1048576;
		$bytesInKilobyte = 1024;

		if ($size >= $bytesInGygabyte * 10)
			return (int)($size / $bytesInGygabyte) . $words[2];

		if ($size >= $bytesInGygabyte)
			return ((int)($size / $bytesInGygabyte * 10) / 10) . $words[2];

		if ($size >= $bytesInMegabyte * 10)
			return (int)($size / $bytesInMegabyte).$words[1];

		if ($size >= $bytesInMegabyte / 5)
			return ((int)($size / $bytesInMegabyte * 10) / 10) . $words[1];

		if ($size >= $bytesInKilobyte)
			return (int)($size / $bytesInKilobyte) . $words[0];

		if ($size >= $bytesInKilobyte / 5)
			return ((int)($size / $bytesInKilobyte * 10) / 10) . $words[0];

		return $size;
	}
}

?>